package com.imagine.service.impl;

import com.imagine.common.Global;
import com.imagine.dal.QuerySinaStocks;
import com.imagine.entity.BaseResult;
import com.imagine.entity.CapitalAccount;
import com.imagine.entity.Delegation;
import com.imagine.entity.Stock;
import com.imagine.entity.vo.CapitalVo;
import com.imagine.mapper.CapitalAccountMapper;
import com.imagine.mapper.DelegationMapper;
import com.imagine.mapper.OptionalStocksMapper;
import com.imagine.service.CapitalAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by liangyue on 2017/9/26.
 */
@Service
public class CapitalAccountServiceImpl implements CapitalAccountService {
    @Autowired
    CapitalAccountMapper accountMapper;

    @Autowired
    DelegationMapper delegationMapper;

    @Autowired
    OptionalStocksMapper optionalStocksMapper;

    @Autowired
    QuerySinaStocks querySinaStocks;

    @Override
    public CapitalAccount getCapitalAccountById(String cAccountid) {

        CapitalAccount capitalAccount = accountMapper.getCapitalAccountById(cAccountid);
        return capitalAccount;
    }

    @Override
    public void insertCapitalAccount(CapitalAccount account){
        accountMapper.insertCapitalAccount(account);
    }


    @Override
    public void updateRemainOrUsable(int remainOrUsable, String cAccountId, String operator, double amount){
        HashMap map = new HashMap();
        map.put("cAccountId", cAccountId);
        map.put("amount", amount);
        //remainOrUsable = 0表示更改总余额，remainOrUsable = 1表示更改可用余额
        if (remainOrUsable == 0){
            if (operator.equals("+")){
                accountMapper.addRemain(map);
            }else {
                accountMapper.subRemain(map);
            }
        }else {
            if (operator.equals("+")){
                accountMapper.addRemainUsable(map);
            }
            else {
                accountMapper.subRemainUsable(map);
            }

        }
    }

    @Override
    public BaseResult getCapitalFlowByCId(String cAccountId){
        BaseResult result = new BaseResult();
        ArrayList<CapitalVo> vos= delegationMapper.getDelegationsAboutCapitalFlowByCId(cAccountId);
        result.success(vos);
        return result;
    }

    @Override
    public BaseResult insertOptionalStock(String cAccountId, String securityId){
        BaseResult result = new BaseResult();
        HashMap<String, String> map = new HashMap<>();
        map.put("cAccountId", cAccountId);
        map.put("securityId", securityId);
        optionalStocksMapper.insertOptionalStocks(map);
        return result;
    }

    @Override
    public BaseResult deleteOptionalStock(String cAccountId, String securityId){
        BaseResult result = new BaseResult();
        HashMap<String, String> map = new HashMap<>();
        map.put("cAccountId", cAccountId);
        map.put("securityId", securityId);
        optionalStocksMapper.deleteOptionalStocks(map);
        return result;
    }

    @Override
    public BaseResult getAllOptionalStocks(String cAccountId){
        BaseResult result = new BaseResult();
        ArrayList<Stock> stocks = new ArrayList<>();
        ArrayList<String> securityIds = optionalStocksMapper.getAllOptionalStocks(cAccountId);
        for (String id :securityIds){
            try {
                Stock stock = querySinaStocks.querySinaStocks(id);
                stocks.add(stock);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        result.success(stocks);
        return result;
    }
}


